import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controller/login_controller.dart';

class LoginView extends StatelessWidget {
  const LoginView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    LoginController loginController = Get.put(LoginController());
    return Scaffold(
      body: SafeArea(
        top: true,
        bottom: true,
        child: Form(
          key: loginController.formKey,
          child: Center(
            child: Container(
              padding: const EdgeInsets.all(8.0),
              width: Get.width * 7 / 10,
              height: Get.height - 100,
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: const [
                      FlutterLogo(size: 80),
                      Text(
                        "SweetIM",
                        style: TextStyle(
                            fontFamily: 'Lobster',
                            fontSize: 40,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                  const SizedBox(height: 30),
                  Row(
                    children: [
                      Expanded(
                        child: TextFormField(
                          keyboardType: TextInputType.phone,
                          decoration: const InputDecoration(
                            border: OutlineInputBorder(),
                            labelText: '手机号',
                          ),
                          controller: loginController.unameController,
                          validator: (value) {
                            if (value == null || value.isEmpty) {
                              return '请输入手机号';
                            }
                            return null;
                          },
                        ),
                      )
                    ],
                  ),
                  const SizedBox(height: 20),
                  Row(
                    children: [
                      Expanded(
                        child: TextFormField(
                          decoration: const InputDecoration(
                            border: OutlineInputBorder(),
                            labelText: '密码',
                          ),
                          controller: loginController.pwdController,
                          obscureText: true,
                          validator: (value) {
                            if (value == null || value.isEmpty) {
                              return '请输入密码';
                            }
                            return null;
                          },
                        ),
                      )
                    ],
                  ),
                  const SizedBox(height: 20),
                  Obx(
                    () => (loginController.loading.value
                        ? const CircularProgressIndicator()
                        : Row(
                            children: [
                              Expanded(
                                child: SizedBox(
                                  child: ElevatedButton(
                                    onPressed: () async {
                                      if (!loginController.formKey.currentState!
                                          .validate()) {
                                        return;
                                      }
                                      loginController.login(
                                          loginController.unameController.text,
                                          loginController.pwdController.text);
                                    },
                                    child: const Text('登 录'),
                                  ),
                                  height: 40,
                                ),
                              ),
                            ],
                          )),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
